import hashlib
import uuid
import json
#import pyudev
import psutil
import datetime
import time  
import os
import random 
from Lib.AlchemyJsonEncoder import *

"""通用工具类"""
class Utils(object):
    def __init__(self, *args, **kwargs):
        return super().__init__(*args, **kwargs)

    #MD5加密
    def MD5(str):
        # 创建md5对象
        hl = hashlib.md5()
        hl.update(str.encode(encoding='utf-8'))
        return hl.hexdigest()

    #获取唯一识别码
    def UUID():
        return uuid.uuid1()

    #生成统一格式接口数据
    def ResultData(status,message,data=""):
        return json.dumps({"status":status,"message":message,"data":data},cls=AlchemyEncoder)

    #按时间生成随机文件名
    def getFileName():
        nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S")#生成当前的时间
        randomNum = random.randint(0,100)#生成随机数n,其中0<=n<=100
        if randomNum <= 10:  
            randomNum = str(0) + str(randomNum)  
        uniqueNum = str(nowTime) + str(randomNum)  
        return uniqueNum 

    #获取当前插入U盘路径
    def getUDiskPath():
        #context = pyudev.Context()
        #removable = [device for device in context.list_devices(subsystem='block', DEVTYPE='disk') if device.attributes.asstring('removable') == "1"]
        #for device in removable:
        #    partitions = [device.device_node for device in context.list_devices(subsystem='block', DEVTYPE='partition', parent=device)]
        #    #print("All removable partitions: {}".format(",
        #    #".join(partitions)))
        #    #print("Mounted removable partitions:")
        #    for p in psutil.disk_partitions():
        #        if p.device in partitions:
        #            return p.mountpoint
        return ""
    #创建文件夹
    def mkdir(path):  
        folder = os.path.exists(path)  
        if not folder:                   
            os.makedirs(path)            
